﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace LogAnalytics.Edit
{
    public partial class PageFilter : QT.Windows.Shell.GlassForm
    {
        public PageFilter()
        {
            InitializeComponent();
            loadForm();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            this.statusStrip1.BackColor = AeroColor;
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            this.pFilter.Location = new Point((this.ClientSize.Width - this.pFilter.Width) / 2, (this.ClientSize.Height - this.pFilter.Height) / 2);
        }

        #region Event

        #region ComboBox

        private void cbbSiteList_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = LogBase.Filter.getAll(this.cbbSiteList.SelectedValue.ToString());
            this.dgvList.AutoGenerateColumns = false;
            this.dgvList.Columns[0].DataPropertyName = "url";
            this.dgvList.Columns[1].DataPropertyName = "comment";
            this.dgvList.Columns[2].DataPropertyName = "status";
            this.dgvList.Columns[3].DataPropertyName = "guid";
            this.dgvList.Columns[4].DataPropertyName = "Enable";
            this.dgvList.DataSource = dt;
            this.dgvList.AutoResizeColumns();
        }

        #endregion

        #region GlassButton

        private void gbtnAdd_Click(object sender, EventArgs e)
        {
            this.lblTitle.Text = "新增过滤页面信息";
            this.lblSiteName.Text = this.cbbSiteList.GetItemText(this.cbbSiteList.SelectedItem);
            this.cbEnable.Checked = true;
            this.tbUrl.Text = "";
            this.tbComment.Text = "";
            this.pFilter.Tag = "";
            this.pFilter.Visible = true;
        }

        private void gbtnModify_Click(object sender, EventArgs e)
        {
            if (this.dgvList.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要修改的记录");
                return;
            }
            if (this.dgvList.SelectedRows.Count > 1)
            {
                MessageBox.Show("每次只能修改一个页面");
                return;
            }

            this.lblTitle.Text = "修改过滤页面信息";
            this.lblSiteName.Text = this.cbbSiteList.GetItemText(this.cbbSiteList.SelectedItem);
            this.cbEnable.Checked = this.dgvList.SelectedRows[0].Cells[4].Value.ToString().ToLower() == "true" ? true : false;
            this.tbUrl.Text = this.dgvList.SelectedRows[0].Cells[0].Value.ToString();
            this.tbComment.Text = this.dgvList.SelectedRows[0].Cells[1].Value.ToString();
            this.pFilter.Tag = this.dgvList.SelectedRows[0].Cells[3].Value.ToString().Trim();
            this.pFilter.Visible = true;
        }

        private void gbtnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgvList.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要删除的记录");
                return;
            }

            List<string> guids = new List<string>();
            foreach (DataGridViewRow row in this.dgvList.SelectedRows)
            {
                guids.Add(row.Cells[3].Value.ToString());
            }

            try
            {
                if (LogBase.Filter.Delete(guids) > 1) MessageBox.Show("删除完成");
                else MessageBox.Show("操作失败");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            cbbSiteList_SelectedIndexChanged(sender, e);
        }

        private void gbtnSave_Click(object sender, EventArgs e)
        {
            if (this.tbUrl.Text.Length == 0)
            {
                MessageBox.Show("请输入URL地址");
                return;
            }
            Regex regex = new Regex(@"^/.*");
            if (!regex.IsMatch(this.tbUrl.Text))
            {
                MessageBox.Show("URL地址格式不正确");
                return;
            }

            try
            {
                int rval = 0;
                if (this.pFilter.Tag == null || this.pFilter.Tag.ToString().Length == 0) rval = LogBase.Filter.Add(this.cbbSiteList.SelectedValue.ToString(), this.tbUrl.Text, this.tbComment.Text, this.cbEnable.Checked ? "1" : "0");
                else rval = LogBase.Filter.Update(this.pFilter.Tag.ToString(), this.cbbSiteList.SelectedValue.ToString(), this.tbUrl.Text, this.tbComment.Text, this.cbEnable.Checked ? "1" : "0");

                switch (rval)
                {
                    case -2:
                        MessageBox.Show("URL地址已存在");
                        return;
                    case -1:
                    case 0:
                        MessageBox.Show("操作失败");
                        return;
                    default:
                        MessageBox.Show("操作成功");
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }

            cbbSiteList_SelectedIndexChanged(sender, e);
            this.pFilter.Visible = false;
        }

        private void gbtnClose_Click(object sender, EventArgs e)
        {
            this.lblTitle.Text = "";
            this.lblSiteName.Text = "";
            this.tbUrl.Text = "";
            this.tbComment.Text = "";
            this.pFilter.Visible = false;
        }

        #endregion

        #region DataGridView
        
        private void dgvList_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            this.tsslMsg.Text = string.Format("当前站点：{0}，共{1}条记录", this.cbbSiteList.GetItemText(this.cbbSiteList.SelectedItem), this.dgvList.RowCount);
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1) return;

            try
            {
                LogBase.Filter.UpdateStatus(this.dgvList.Rows[e.RowIndex].Cells[3].Value.ToString(), this.cbbSiteList.SelectedValue.ToString(), this.dgvList.Rows[e.RowIndex].Cells[4].Value.ToString().ToLower() == "true" ? "0" : "1");
                cbbSiteList_SelectedIndexChanged(sender, new EventArgs());
                this.dgvList.Rows[0].Selected = false;
                this.dgvList.Rows[e.RowIndex].Selected = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        #endregion

        #endregion

        #region Method

        private void loadForm()
        {
            DataTable dt = LogBase.SiteManage.getAll();
            this.cbbSiteList.DisplayMember = "sitename";
            this.cbbSiteList.ValueMember = "guid";
            this.cbbSiteList.DataSource = dt;
        }

        #endregion
    }
}
